<template>
    <div>
        <el-drawer
            title=""
            :visible.sync="drawerVisible"
            :size="1400"
            :wrapper-closable="false"
            :with-header="false"
        >
            <div class="drawer-box">
                <div class="handle-btn">
                    <i class="el-icon-close" title="关闭" @click="drawerVisible=false" />
                </div>
                <div class="top-header">
                    <div class="title">
                        <div>
                            <span class="name">{{ title }}</span>
                        </div>
                    </div>
                </div>
                <!-- 筛选栏 -->
                <search-bar>
                    <el-form :model="search" size="small" label-width="100px">
                        <el-row>
                            <el-col :span="6">
                                <el-form-item label="创建时间">
                                    <el-date-picker
                                        v-model="search.date"
                                        type="daterange"
                                        range-separator="-"
                                        start-placeholder="开始日期"
                                        end-placeholder="结束日期"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        @change="handleDateChange"
                                    />
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="跟进人">
                                    <el-select v-model="search.memberIds" clearable placeholder="请选择跟进人">
                                        <el-option v-for="(item, index) in memberList" :key="index" :label="item.realname" :value="item.id" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item>
                                    <el-button type="primary" @click="getList">查询</el-button>
                                    <el-button @click="handleReset">重置</el-button>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </search-bar>
                <div class="content">
                    <!-- 表格 table clueAssets-->
                    <el-table v-if="name=='clueAssets'" v-loading="loading" :data="dataList" border stripe highlight-current-row>
                        <el-table-column fixed prop="name" label="线索名称">
                            <template #default="scope">
                                <el-button type="text" size="small" @click="detail(scope.row, 'clue')">{{ scope.row.name }}</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column fixed prop="phone" label="手机号" />
                        <el-table-column prop="pool_name" label="线索池" />
                        <el-table-column prop="level_name" label="线索等级" />
                        <el-table-column prop="from_name" label="来源渠道" />
                        <el-table-column prop="created" label="创建时间" />
                        <el-table-column prop="last_log_day" label="跟进记录" />
                        <el-table-column prop="last_log_time" label="最近跟进时间" />
                        <el-table-column prop="member" label="当前跟进人" />
                    </el-table>
                    <!-- 表格 table customerAssets-->
                    <el-table v-if="name=='customerAssets'" v-loading="loading" :data="dataList" border stripe highlight-current-row>
                        <el-table-column fixed prop="name" label="客户名称">
                            <template #default="scope">
                                <el-button type="text" size="small" @click="detail(scope.row, 'custom')">{{ scope.row.name }}</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column fixed prop="phone" label="手机号" />
                        <el-table-column prop="pool_name" label="客户池" />
                        <el-table-column prop="level_name" label="线索等级" />
                        <el-table-column prop="from_name" label="来源渠道" />
                        <el-table-column prop="created" label="创建时间" />
                        <el-table-column prop="last_log_day" label="跟进记录" />
                        <el-table-column prop="last_log_time" label="最近跟进时间" />
                        <el-table-column prop="member" label="当前跟进人" />
                    </el-table>
                    <!-- 表格 table businessOpportunityDeal-->
                    <el-table v-if="name=='businessOpportunityDeal'" v-loading="loading" :data="dataList" border stripe highlight-current-row>
                        <el-table-column fixed prop="member" label="跟进人" />
                        <el-table-column fixed prop="name" label="商机名称">
                            <template #default="scope">
                                <el-button type="text" size="small" @click="detail(scope.row, 'niche')">{{ scope.row.name }}</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column fixed prop="user_name" label="客户名称">
                            <template #default="scope">
                                <el-button type="text" size="small" @click="detail(scope.row, 'custom')">{{ scope.row.user_name }}</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column prop="user_phone" label="手机号" width="120" />
                        <el-table-column prop="status" label="所属阶段">
                            <template #default="scope">
                                <span>{{ scope.row.status | statusFilter }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="loan" label="商机金额(元)" />
                        <el-table-column prop="commission" label="预测回佣金额(元)" width="160" />
                        <el-table-column prop="times" label="预计签单时间" width="160" />
                        <el-table-column prop="created" label="创建时间" width="160" />
                        <el-table-column prop="updated" label="最近更新时间" width="160" />
                    </el-table>
                    <!-- 分页 -->
                    <div style="margin-top: 20px; text-align: right;">
                        <el-pagination
                            :current-page="search.pages"
                            :page-size="search.size"
                            :total="search.total"
                            :page-sizes="[10, 20, 50, 100]"
                            background
                            layout="total, sizes, prev, pager, next, jumper"
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                        />
                    </div>
                </div>
            </div>
        </el-drawer>
        <!-- 线索详情 -->
        <clue-detail-drawer ref="clueDetailDrawer" />
        <!-- 客户详情 -->
        <custom-detail-drawer ref="customDetailDrawer" />
        <!-- 商机详情 -->
        <niche-detail-drawer ref="nicheDetailDrawer" />
    </div>
</template>
<script>
import { userListInfo, kehuListInfo, businessListInfo } from '@/api/analysis'
import { memberList } from '@/api/system'
import ClueDetailDrawer from './../../../sales_management/clue/components/DetailDrawer'
import CustomDetailDrawer from './../../../sales_management/custom/components/DetailDrawer'
import NicheDetailDrawer from './../../../sales_management/niche/components/DetailDrawer'
export default {
    components: {
        ClueDetailDrawer,
        CustomDetailDrawer,
        NicheDetailDrawer
    },
    filters: {
        statusFilter(val) {
            const map = {
                '0': '匹配方案',
                '1': '强调优势',
                '2': '签订合同',
                '3': '进行审批',
                '4': '银行放款',
                '5': '回款',
                '6': '赢单',
                '7': '输单'
            }
            return map[val]
        }
    },
    data() {
        return {
            loading: false,
            name: 'clueAssets',
            title: '',
            search: {
                type: '',
                date: [],
                start: '',
                end: '',
                memberIds: '',
                infotype: '',
                s_member_id: '',
                pages: 1,
                size: 10,
                total: 0
            },
            drawerVisible: false,
            id: '',
            info: {},
            dataList: [],
            memberList: []
        }
    },
    methods: {
        showDrawer(row) {
            this.name = row.name,
            this.search.type = row.type || '';
            this.search.infotype = row.infotype;
            this.search.s_member_id = row.id;
            this.search.start = row.start || "";
            this.search.end = row.end || "";
            this.search.date = [ row.start, row.end];
            this.title = `${row.header}${row.typeName}明细`
            this.loading = true;
            this.getOptions();
            this.handleResets();
            this.drawerVisible = true;
        },
        // 切换分页size
        handleSizeChange(val) {
            this.search.size = val;
            this.getList();
        },
        // 切换页数
        handleCurrentChange(val) {
            this.search.pages = val;
            this.getList();
        },
        // 重置筛选条件
        handleResets() {
            this.memberList = [];
            this.search.memberIds = '';
            this.search.pages = 1;
            this.search.total = 0;
            this.getList();
        },
        // 重置筛选条件
        handleReset() {
            this.memberList = [];
            this.search.start = '';
            this.search.end = '';
            this.search.memberIds = '';
            this.search.pages = 1;
            this.search.total = 0;
            this.getList();
        },
        getList() {
            if (this.name == 'clueAssets') {
                userListInfo(this.search).then(res => {
                    this.dataList = res.data.list;
                    this.search.total = Number(res.data.count);
                })
            } else if (this.name == 'customerAssets') {
                kehuListInfo(this.search).then(res => {
                    this.dataList = res.data.list;
                    this.search.total = Number(res.data.count);
                })
            } else if (this.name == 'businessOpportunityDeal') {
                this.search.create_start = this.search.start;
                this.search.create_end = this.search.end;
                businessListInfo(this.search).then(res => {
                    this.dataList = res.data.list;
                    this.search.total = Number(res.data.count);
                })
            }
            this.loading = false;
        },
        // 详情
        detail(row, type) {
            console.log(row, type);
            if (type == 'clue') {
                this.$refs.clueDetailDrawer.showDialog(row, {});
            } else if (type == 'custom') {
                this.$refs.customDetailDrawer.showDialog({id: row.user_id}, {});
            } else if (type == 'niche') {
                this.$refs.nicheDetailDrawer.showDialog(row, {});
            }

        },
        handleDateChange(e) {
            this.search.start = e ? e[0] : ''
            this.search.end = e ? e[1] : ''
        },
        // 获取 options
        getOptions() {
            memberList().then(res => {
                this.memberList = res.data.list || []
            })
        }
    }
}
</script>
<style lang="scss" scoped>
::v-deep .el-drawer__body {
    background: transparent;
    overflow: hidden;
    position: relative;
    padding-left: 40px;
    padding-top: 70px;
    box-sizing: border-box;
}
::v-deep .el-drawer {
    background: transparent;
    box-shadow: none;
}
.drawer-box {
    height: 100%;
    background: rgb(242, 245, 251);
    border-radius: 4px 0 0 4px;
    display: flex;
    flex-direction: column;
    .handle-btn {
        width: 40px;
        // height: 120px;
        padding: 15px 0;
        background: #fff;
        position: absolute;
        top: 110px;
        left: 0;
        border-radius: 4px 0 0 4px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        cursor: pointer;
        i {
            height: 30px;
            line-height: 30px;
            &:hover {
                color: #409eff;
            }
        }
    }
}
.top-header {
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
    font-size: 17px;
    color: #666;
    background: #fff;
    .title {
        width: 100%;
        display: flex;
        justify-content: space-between;
        .name {
            font-weight: 600;
            color: #333;
            font-size: 16px;
            margin-right: 30px;
            line-height: 32px;
        }
        .member {
            margin-right: 20px;
            color: #999;
            span {
                color: #333;
            }
        }
    }
    .tags {
        margin-top: 10px;
        .item {
            margin-right: 10px;
        }
    }
    .step {
        margin-top: 10px;
        cursor: pointer;
    }
}
.content {
    flex: 1;
    padding: 20px;
    background: #fff;
    margin-top: 10px;
    overflow: auto;
    .card {
        background: #fff;
        padding: 5px 15px 10px;
        box-sizing: border-box;
    }
}
</style>
